--声明游标变量
DECLARE @TNAMES CURSOR
DECLARE @TNAME VARCHAR(20)=NULL

--创建游标
SET @TNAMES = CURSOR SCROLL KEYSET FOR
SELECT name FROM sys.sysobjects WHERE  xtype='U'

--打开游标
OPEN @TNAMES

--从游标变量中读取值
FETCH NEXT FROM  @TNAMES INTO @TNAME

--判断FETCH语句是否执行成功
WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT @TNAME
	EXEC('SELECT * FROM ' + @TNAME)	--用变量做数据库查询名时要用exec(加语句)来执行语句
	--读取游标变量中的数据
	FETCH NEXT FROM  @TNAMES INTO @TNAME
	
END
--关闭游标
CLOSE @TNAMES
--释放游标
DEALLOCATE @TNAMES